import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)

const store = new Vuex.Store({
    state: {
        hasBeenShowBooks: [],
        loadingLessonSet: [],//正在下载
        completedBook: [],//已完成
        bookId: '',//方便使用，后续可以不用在查询数据库
        userId: '',//方便使用，后续可以不用在查询数据库
        giveLessons: false,//授课模式
        becomeVip:false, //非vip
        cloudprogressVal:0,
    },
    mutations: {
        setGiveLessons (state, val) {
            state.giveLessons = val
        },
        setBookId (state, bookId) {
            state.bookId = bookId
        },
        setUserId (state, userId) {
            state.userId = userId
        },
        pushBooks (state, book) {
            state.hasBeenShowBooks.push(book)
        },
        updateBooks (state, id) {
            state.hasBeenShowBooks.map(item => {
                if (item.id === id) {
                    item.timestamp = Date.now()
                }
            })
        },
        addLoadingLesson (state, lesson) {
            if (Array.isArray(lesson)) {
                state.loadingLessonSet.push(...lesson)
            } else {
                state.loadingLessonSet.push(lesson)
            }
        },
        //确保传入的lesson包含id
        changeLoadingStatus (state, lesson) {
            let targetIndex = -1
            let obj = {}
            state.loadingLessonSet.some((item, index) => {
                if (lesson.id) {
                    if (item.id === lesson.id) {
                        targetIndex = index
                        obj = item
                        return true
                    }
                } else {
                    if (item.name === lesson.name) {
                        targetIndex = index
                        obj = item
                        return true
                    }
                }
            })
            if (targetIndex !== -1) {
                Object.keys(lesson).forEach(key => {
                    (lesson[key] !== undefined) && (obj[key] = lesson[key])
                })
                state.loadingLessonSet.splice(targetIndex, 1, obj)
            }

        },
        removeLoadingBook (state, id) {
            if (id === 'all') {
                state.loadingLessonSet = []
                return
            }
            let targetIndex = -1
            state.loadingLessonSet.some((item, index) => {
                if (item.id === id) {
                    targetIndex = index
                    return true
                }
            })
            targetIndex !== -1 && state.loadingLessonSet.splice(targetIndex, 1)
        },
        addCompletedBook (state, lesson) {
            if (Array.isArray(lesson)) {
                state.completedBook.push(...lesson)
            } else {
                state.completedBook.push(lesson)
            }
        },
        changecloudprogressVal (state, val) {
            state.cloudprogressVal = val
        },
        changeCompletedBook (state, lesson) {
            let targetIndex = -1
            state.completedBook.some((item, index) => {
                if (lesson.lessonId === item.lessonId) {
                    console.log(lesson);
                    targetIndex = index
                    return true
                }
            })
            if (targetIndex === -1) {
                state.completedBook.push(lesson)
            } else {
                state.completedBook.splice(targetIndex, 1, lesson)
            }
        },
        removeCompletedBook (state, id) {
            let targetIndex = -1
            state.completedBook.some((item, index) => {
                if (item.lessonId === id) {
                    targetIndex = index
                    return true
                }
            })
            targetIndex !== -1 && state.completedBook.splice(targetIndex, 1)
        },
        clearDownLoadCenter (state) {
            state.completedBook = []
            state.completedBook = []
            state.completedBook = []
        }

    }
})

export default store
